package com.gongpingjialoan.activity.car;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.adapter.PayCarListAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.bean.car.ChatCarBean;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.SharedPreferencesUtil;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.view.SelectDateDialog;
import com.gongpingjialoan.widget.LoadingDialog;
import com.gongpingjialoan.wxapi.WXPayEntryActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/11.
 */
public class PayDetailActivity extends BaseActivity {

    private Button btn_pay;
    private RelativeLayout time_rl;
    private ListView pay_car_list;
    private TextView car_name_tv;
    private PayCarListAdapter mChatCarListAdapter;
    private List<ChatCarBean> carBuyList = new ArrayList<>();
    private String carName;
    private String car_id;
    private LinearLayout mTipsRl;
    private TextView time_day_tv;
    /**
     * 日期选择弹框
     */
    private SelectDateDialog dateDialog;
    private String view_date;
    private int lastPosition;
    private String mType;
    private NetDataJson mNetDataJson;
    private Dialog mLoadingDialog;
    private int mPrice;
    private int[] mPriceArray;
    private TextView price_tv;
    private Intent intent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        isSendData = true;
        isScrollPage = true;
        super.onCreate(savedInstanceState);
        intent = getIntent();
        action = intent.getStringExtra("action_type");
        setContentView(R.layout.activity_pay_detail);
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        if(mNetDataJson != null){
            mNetDataJson.cancelTask();
        }
        super.onDestroy();
    }

    private void initData() {
        mLoadingDialog = new LoadingDialog(this);

        if (intent != null) {
            carBuyList = (List<ChatCarBean>) intent.getSerializableExtra("myViewedList");
            mType = intent.getStringExtra("type");
            if (carBuyList == null) {
                carName = intent.getStringExtra("carName");
                car_id = intent.getStringExtra("car_id");
            } else if (carBuyList.size() == 1) {
                carName = carBuyList.get(0).title;
                car_id = carBuyList.get(0).car_id;
            } else if (carBuyList.size() > 1) {
                carName = carBuyList.get(0).title;
                car_name_tv.setVisibility(View.GONE);
                mChatCarListAdapter = new PayCarListAdapter(carBuyList, this);
                pay_car_list.setAdapter(mChatCarListAdapter);
                mChatCarListAdapter.notifyDataSetChanged();
            }
            car_name_tv.setText(carName);
        }

        getPayPrices();

    }

    private void initView() {
        btn_pay = (Button) findViewById(R.id.btn_pay);
        time_rl = (RelativeLayout) findViewById(R.id.time_rl);
        pay_car_list = (ListView) findViewById(R.id.pay_car_list);
        car_name_tv = (TextView) findViewById(R.id.car_name_tv);
        mTipsRl = (LinearLayout) findViewById(R.id.tips_rl);
        time_day_tv = (TextView) findViewById(R.id.time_day_tv);
        price_tv = (TextView) findViewById(R.id.price_tv);
        time_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != dateDialog) {
                    dateDialog.show();
                }
            }
        });
        dateDialog = new SelectDateDialog(this, 7, new SelectDateDialog.OnDialogCallBack() {
            @Override
            public void onDateBack(String date , String s) {
                view_date = date;
                time_day_tv.setText(s);
            }
        });


        if ((boolean) SharedPreferencesUtil.get(this, "pay_notification_show", true)) {
            findViewById(R.id.tips_rl).setVisibility(View.VISIBLE);
        }

        findViewById(R.id.tip_close_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                findViewById(R.id.tips_rl).setVisibility(View.GONE);
                SharedPreferencesUtil.put(PayDetailActivity.this, "pay_notification_show", false);
            }
        });

        pay_car_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (lastPosition == position) return;
                int size = carBuyList.size();
                for (int i = 0; i < size; i++) {
                    if (position == i) {
                        carName = carBuyList.get(position).title;
                        car_id = carBuyList.get(position).car_id;
                        mPrice = mPriceArray[position];
                        price_tv.setText(String.valueOf(mPrice/100.0));
                    }
                }
                mChatCarListAdapter.setData(carBuyList, position);
                lastPosition = position;

            }
        });
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(carName)) {
                    showTips(2, "请选择车型");
                    return;
                }
                if (TextUtils.isEmpty(view_date)) {
                    showTips(2, "请选择看车时间");
                    return;
                }
                StepMonitor.getInstance().addMonitor(new Monitor("click", action, System.currentTimeMillis(), 1.0f));
                Intent i = new Intent(PayDetailActivity.this, WXPayEntryActivity.class);
                Bundle b = new Bundle();
                b.putString("carName", carName);
                b.putString("car_id", car_id);
                b.putString("view_date", view_date);
                b.putString("type", mType);
                b.putInt("price", mPrice);
                i.putExtras(b);
                PayDetailActivity.this.startActivity(i);

            }
        });
    }


    private void getPayPrices() {
        mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                mLoadingDialog.dismiss();
                JSONObject jsonObject = null;
                try {
                    jsonObject = json.getJSONObject("data");

                    if (jsonObject == null || 0 == jsonObject.length()) {
                        return;
                    }
                    if (carBuyList == null) {
                            mPrice = jsonObject.getInt(car_id);
                            price_tv.setText(String.valueOf(mPrice/100.0));
                    } else {
                        int size = carBuyList.size();
                        mPriceArray = new int[size];
                        for (int i = 0; i < size; i++) {
                            mPriceArray[i] = jsonObject.getInt(carBuyList.get(i).car_id);
                        }
                            mPrice = mPriceArray[0];
                            price_tv.setText(String.valueOf(mPrice/100.0));
                    }
                } catch (JSONException e) {
                    com.gongpingjialoan.utility.Utils.LogD("onDataJsonUpdate JSONException " + e.getMessage());
                }
            }

            @Override
            public void onDataJsonError(String errorMessage) {
                mLoadingDialog.dismiss();
            }

        });
        mLoadingDialog.show();
        if (carBuyList == null) {
            if ("car_ordered_record".equals(mType)) {
                mNetDataJson.addParam("car_ids", car_id);
                mNetDataJson.addParam("type", "car_ordered_record");
            } else {
                mNetDataJson.addParam("promo_car_ids", car_id);
                mNetDataJson.addParam("type", "promo_car_ordered_record");
            }
        } else {
            StringBuilder car_ids = new StringBuilder();
            int size = carBuyList.size();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    String carId = carBuyList.get(i).car_id;
                    if (!TextUtils.isEmpty(carId)) {
                        car_ids.append(carId);
                        if (i < size - 1) {
                            car_ids.append(",");
                        }
                    }

                }
            }
            mNetDataJson.addParam("car_ids", car_ids.toString());
            mNetDataJson.addParam("type", "car_ordered_record");
        }
        mNetDataJson.setUrl(API.get_pay_prices);
        mNetDataJson.request("get");
    }
}
